package com.zmn.brs.services.impl.threshold;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zmn.brs.common.enums.ThresholdExpressionEnum;
import com.zmn.brs.model.entity.threshold.ThresholdBase;
import com.zmn.brs.model.query.rule.EarlyWarningConfig;
import com.zmn.brs.model.vo.rule.ObjRuleVO;
import com.zmn.brs.model.vo.rule.objective.ObjectiveRuleVO;
import com.zmn.brs.services.interfaces.event.EventService;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import com.zmn.brs.model.entity.threshold.Threshold;
import com.zmn.brs.model.entity.threshold.ThresholdQuery;
import com.zmn.brs.services.interfaces.threshold.ThresholdService;
import com.zmn.brs.persistence.interfaces.threshold.ThresholdDao;

/**
 * 描述: 阀值基础接口实现类
 *
 * @author xujie
 * @since  2021-03-17 13:55:07
 **/
@Slf4j
@Service
public class ThresholdServiceImpl implements ThresholdService {

    private static final String TAG = "阀值基础服务接口";

    @Resource
    private ThresholdDao thresholdDao;

    @Resource
    private EventService eventService;

    /**
     * 描述: 根据id查询数据
     *
     * @param thresholdId id
     * @author xujie
     * @since 2021-03-17 13:55:07
     **/
    @Override
    @ReadOnlyConnection
    public Threshold findById(Integer thresholdId) {

        Assert.notNull(thresholdId, TAG + "参数 thresholdId 不能为空");
        return thresholdDao.findById(thresholdId);
    }

    /**
     * 描述: 根据规则id查询数据
     *
     * @param ruleId 规则ID
     * @return Threshold
     * @author chongw
     * @since 2021-07-12 18:55:07
     **/
    @Override
    @ReadOnlyConnection
    public Threshold findCommonThresholdByRuleId(Integer ruleId){
        Assert.notNull(ruleId, TAG + "参数 ruleId 不能为空");
        return thresholdDao.findCommonThresholdByRuleId(ruleId);
    }

    /**
     * 描述: 根据规则id及目标责任人查询数据
     *
     * @param ruleId 规则ID
     * @return Threshold
     * @author chongw
     * @since 2021-07-20 18:55:07
     **/
    @Override
    @ReadOnlyConnection
    public Threshold findThresholdByStaffId(Integer ruleId , Integer staffId){
        Assert.notNull(ruleId, TAG + "参数 ruleId 不能为空");
        Assert.notNull(staffId, TAG + "参数 staffId 不能为空");
        return thresholdDao.findThresholdByStaffId(ruleId, staffId);
    }

    /**
     * 描述: 根据阀值ID列表查询阀值基础数据
     *
     * @param  thresholdIdList 阀值ID列表
     * @return List<ThresholdBase>
     * @author xujie
     * @since 2021-03-18 18:44:07
     **/
    @Override
    @ReadOnlyConnection
    public List<ThresholdBase> listByThresholdIdList(List<Integer> thresholdIdList) {

        if (thresholdIdList == null || thresholdIdList.size() == 0) {
            return new ArrayList<>();
        }
        return thresholdDao.listByThresholdIdList(thresholdIdList);
    }

    /**
     * 描述: 根据规则ID列表查询阀值基础数据
     *
     * @param  ruleIdList 规则ID列表
     * @return List<ThresholdBase>
     * @author xujie
     * @since 2021-03-17 14:57:07
     **/
    @Override
    @ReadOnlyConnection
    public List<ThresholdBase> listByRuleIdList(List<Integer> ruleIdList) {

        if (ruleIdList == null || ruleIdList.size() == 0) {
            return null;
        }
        return thresholdDao.listByRuleIdList(ruleIdList);
    }

    /**
     * 描述: 按条件查询数据 支持分页
     *
     * @param query 查询对象
     * @author xujie
     * @since 2021-03-17 13:55:07
     **/
    @Override
    @ReadOnlyConnection
    public List<Threshold> listPageByQuery(ThresholdQuery query) {

        Assert.notNull(query, TAG + "参数 query 不能为空");
        query.setTotalCount(thresholdDao.countByQuery(query));
        return thresholdDao.listPageByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author xujie
     * @since 2021-03-17 13:55:07
     **/
    @Override
    @ReadOnlyConnection
    public List<Threshold> listByQuery(ThresholdQuery query) {

        Assert.notNull(query, TAG + "参数 query 不能为空");
        return thresholdDao.listByQuery(query);
    }

    @Override
    public List<ThresholdBase> getThresholdBasesByEventCodeAndThresholdName(Integer eventCode, String thresholdName) {

        return thresholdDao.findThresholdBasesByEventCodeAndThresholdName(eventCode, thresholdName);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author xujie
     * @since 2021-03-17 13:55:07
     **/
    @Override
    @ReadOnlyConnection
    public Integer countByQuery(ThresholdQuery query) {

        Assert.notNull(query, TAG + "参数 query 不能为空");
        return thresholdDao.countByQuery(query);
    }

    /**
     * 描述: 根据id修改数据 id为必传
     *
     * @param threshold 待修改数据
     * @return java.lang.Integer
     * @author xujie
     * @since 2021-03-17 13:55:07
     **/
    @Override
    public Integer updateByKey(Threshold threshold) {

        Assert.notNull(threshold, TAG + "参数 threshold 不能为空");
        Assert.notNull(threshold.getThresholdId(), TAG + "字段 thresholdId 不能为空");
        return thresholdDao.updateByKey(threshold);
    }

    /**
     * 描述: 批量修改数据
     * @param thresholdList 数据集合
     * @return java.lang.Integer 成功个数
     * @author xujie
     * @since  2021-03-17 13:55:07
     */
    @Override
    public Integer updateBatchByKey(List<Threshold> thresholdList) {

        if (thresholdList == null || thresholdList.size() == 0) {
            return null;
        }
        return thresholdDao.updateBatchByKey(thresholdList);
    }

    /**
     * 描述: 新增数据
     *
     * @param threshold 待新增数据
     * @return java.lang.Integer 主键key
     * @author xujie
     * @since  2021-03-17 13:55:07
     **/
    @Override
    public Integer insert (Threshold threshold) {

        Assert.notNull(threshold, TAG + "参数 threshold 不能为空");
        return thresholdDao.insert(threshold);
    }

    /**
     * 描述: 批量新增数据
     * @param thresholdList 待新增数据集合
     * @return java.lang.Integer 成功个数
     * @author xujie
     * @since  2021-03-17 13:55:07
     */
    @Override
    public Integer insertBatch(List<Threshold> thresholdList) {

        if (thresholdList == null || thresholdList.size() == 0) {
            return null;
        }
        return thresholdDao.insertBatch(thresholdList);
    }

    /**
     * 描述: 根据主键删除数据
     *
     * @param thresholdId 主键
     * @return 执行结果码
     * @author xujie
     * @since 2021-03-17 13:55:07
     */
    @Override
    public Integer deleteByKey(Integer thresholdId) {

        Assert.notNull(thresholdId, TAG + "参数 thresholdId 不能为空");
        return thresholdDao.deleteByKey(thresholdId);
    }

    /**
     * 描述: 根据主键批量删除数据
     *
     * @param thresholdIdList 待新增数据集合
     * @return java.lang.Integer 成功个数
     * @author xujie
     * @since  2021-03-17 13:55:07
     */
    @Override
    public Integer deleteBatchByKey(List<Integer> thresholdIdList) {

        if (thresholdIdList == null || thresholdIdList.size() == 0) {
            return null;
        }
        return thresholdDao.deleteBatchByKey(thresholdIdList);
    }

    /**
     * 修改规则值域
     * @param objRuleVO
     * @return
     */
    @Override
    public Integer updateRuleThreshold(ObjRuleVO objRuleVO){
        //设置值域
        List<EarlyWarningConfig> warningConfigList = eventService.setEarlyWarningConfig(objRuleVO.getValue1(), objRuleVO.getValue2(), objRuleVO.getValue3());
        Threshold threshold = thresholdDao.findById(objRuleVO.getThresholdId());
        ObjectiveRuleVO vo = JSONObject.parseObject(threshold.getThresholdValue(), ObjectiveRuleVO.class);
        vo.setList(warningConfigList);
        threshold.setThresholdId(objRuleVO.getThresholdId());
        threshold.setThresholdValue(JSONObject.toJSONString(vo));
        threshold.setUpdateTime(DateUtil.getNow());
        threshold.setUpdater(objRuleVO.getMcStaffName());
        return this.updateByKey(threshold);
    }

}